﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CueMounter.DokanStructure;

namespace CueMounter {
	class DokanReadOnlyTextFile : DokanFile {
		private string contents;
		private byte[] bytes;

		public DokanReadOnlyTextFile(string name, string contents) {
			Name = name;
			this.contents = contents;
		}

		private void PopulateBytes() {
			if(bytes == null) {
				bytes = Encoding.UTF8.GetBytes(contents);
			}
		}

		public override long Length {
			get {
				PopulateBytes();
				return bytes.Length;
			}
			set
			{
				throw new NotSupportedException();
			}
		}

		public override bool CanRead { get { return true; } }
		public override bool CanWrite { get { return false; } }

		public override long Read(long fileOffset, byte[] buffer, long bufferOffset, long length) {
			PopulateBytes();
			long toCopy = Math.Min(bytes.Length - fileOffset, length);
			Array.Copy(bytes, fileOffset, buffer, bufferOffset, toCopy);
			return toCopy;
		}
	}
}
